package net.wenku8.reader.view.online

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.lifecycle.viewmodel.compose.viewModel
import net.wenku8.reader.cache.ImageCacheHandler
import net.wenku8.reader.ui.lite.LiteLabel
import net.wenku8.reader.ui.lite.LitePanel
import net.wenku8.reader.ui.lite.largePadding
import net.wenku8.reader.ui.lite.mediumPadding
import net.wenku8.reader.ui.lite.onSwipe
import net.wenku8.reader.ui.lite.onTap
import net.wenku8.reader.ui.lite.smallPadding
import net.wenku8.reader.ui.theme.LiteGray
import net.wenku8.reader.view.common.CacheImage
import net.wenku8.reader.view.common.ConfirmLayer
import net.wenku8.reader.view.common.ConfirmViewModel
import net.wenku8.reader.view.common.ReadBgImage
import net.wenku8.reader.view.common.ReadBottom
import net.wenku8.reader.view.common.ReadSettingModel

@Composable
fun OnlineRead() {
    val onlineReadViewModel = viewModel<OnlineReadViewModel>()
    val readSettingModel = viewModel<ReadSettingModel>()
    val confirmViewModel = viewModel<ConfirmViewModel>()
    readSettingModel.initData()
    LaunchedEffect(Unit) {
        onlineReadViewModel.initData()
    }
    if (onlineReadViewModel.loaded){
        LitePanel(onlineReadViewModel.hostState, topBar = false){
            Box(
                modifier = Modifier.statusBarsPadding().fillMaxSize()
                    .background(readSettingModel.bgColor)
            ) {
                ReadBgImage()
                Column(
                    modifier = Modifier
                        .largePadding()
                        .background(Color.Transparent)
                        .onSwipe {
                            onlineReadViewModel.changeChapter(it)
                        }
                        .onTap(onClick = {
                            readSettingModel.show = true
                        })
                ){
                    Box(
                        modifier = Modifier.fillMaxWidth().smallPadding().background(LiteGray)
                    ) {
                        Column(
                            modifier = Modifier.mediumPadding()
                        ) {
                            LiteLabel(text = onlineReadViewModel.chapter.title!!, color = readSettingModel.textColor, fontSize = readSettingModel.fontSize)
                        }
                    }
                    LazyColumn (
                        modifier = Modifier.fillMaxWidth()
                    ){
                        items(items = onlineReadViewModel.textList) { text ->
                            Row(
                                modifier = Modifier.padding(top = readSettingModel.spaceSize)
                            ) {
                                if (onlineReadViewModel.isText){
                                    Text(text = text, color = readSettingModel.textColor, fontSize = readSettingModel.fontSize)
                                }else{
                                    val cacheHandler = remember { ImageCacheHandler(text) }
                                    CacheImage(cacheHandler, onTap = {
                                        confirmViewModel.show("你需要保存图片吗？"){
                                            onlineReadViewModel.saveImageByFile(cacheHandler.getFile())
                                        }
                                    })
                                }
                            }
                        }
                    }

                }
            }
            ReadBottom()
        }
        ConfirmLayer()
    }
}
